package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117624674";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCRxS1tuAjxfIvxwtI617XmoljO0byi8mPXWsCYDWWN47vZmkGp7UptQWpPpgGirK2SGhXtrJ6gTf21CC/3XvXhChOqrRx8xDHH4G8FCUtbOr6ixmiXAVNIqCVbU8frt3n0U+Sb8/nwPcs7XFqcKP3zt/h7NLVofS9hmVldWYcsbOjze9YRuPKlBxsb/KBSyN/lhaSTkgl3HHdKIq7AcQvcdWv+jhk+Rt3+M83C0VhBQuDWKh74DGvwl0VxFMnLEklktG5A/rWnA5b89IF03sN/lSGbN869Sy0kFgvmUgozS4Z/SpNL06JUyfx5dQsu2Zl95z5W/zs7VX2Xk+U28rCRAgMBAAECggEAINBzToysLrhrxGun6akfgKCxPzWFsmTARv1d95ykbNGEOxL3jyoko/2i0fU7zwPyOr52iz9dohaMc1E5gwU0colTLgu0Kv37etQyCb8y4IGT4o6pgrzAo81+Q1XT3mM6J98uylCcxhETC/TZTOgJavDSFeIiqDfuD5l2vYXm4P/4F/IkPVGc9kJ6EC4kXq/BmJIsrJm1Re+UVx/Z9Aus2CMwBzJP3pEtmOiUUqTTSnr7Sk5L9ETzWiVgJlf+zOSdLORgJwdb+g+YPZU2aOmaYQjL/Z2OIKXO8Wi8N0NYN869wNvEqhSdwApvHNANAq2ggHtBCreE+SbC9VoiGFXIAQKBgQDRxq1/bfxZ+0pSoSm4S389YTprW6C75zSFckhbVF042pot7NAhquoerqPW7QiSXgfizina1di26hHAb5KMtEPZTB8NrJ0RMaLAd+d9HZodottphBJYoNieNFYSJe8zFHok4qEGOvvclfQz3Z1rorIdQyhtOINrjPexDKwEgOwsEQKBgQCx4/lRLtYMJT6FfPRjD1q0ZsOXxpKc9J83SunNiGcrdKjL0H26M8jQskbw91Lqpu0VgQqNNkw3HWEgXehjxP/+wjp3ndjRkUVhmJ3SqKaUbejzsFLpahDfFWAgnvSk6tA+YE01t2dZYrmv+z4yRElw2CutQI3UE7WDc8pwulS8gQKBgAXH4dQXJdMc96lbQ5kOi40XdtKqjb1f27Spt5C8vGzqqUEGu52R5i2+RkmP8qKF8NWxNLoRFo3D2YViDbnO5efBD0IDNbjZk1CWfUyXWj3i1QUNXKwQkqeZbZ3GadcQg2Us0Z8oVPR5NWeQfdwPebUQurZ3zjdShSjd3oepPdjhAoGBAJ52wkU7P5maI9Ott9X2e4pwcJPnY/tFPf93UEay15SyWxsUIquYLa8a4G8PImG9STz6sneQgtwl47t9a+JUPuamBoc/qUEl83Q5C334lktqjTVnQLSEs3rGA9eLQcsXEWI/Twgm1H2foGHPoVwN0I4hQucWtkDRPcRXhWJd2dYBAoGBAJcQjJIAMcZGCE556Kyf34dJBsMbL+MHS7HFsu3I+NyfLsPkF8CONMkvikwxPbbO34kmEhyYo9AsTP8xU5MpHt0sCdlz0V9mlgn3ugXicpJfRT+KyEM89A9k4s+mvfahYca3eIOCGiiiRlMWMg6D02NPMKb4uyhF7GPHy1HzOEmm";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxX/Jr+h0ZR6rThk2aFtLTh0WYIcO7vxK6cGPOjVibYIsXk+GzXfb1/dxdvn75l4/u2cqtF80vC3nAu8ifl+CU0K1cPsJzkk1P2hf+cwwMZC/4Q6h/W/QhSAfKQ0JmbbdAthR8SfCQdmkvCSqUXSuFA1fpHJVcWVHspH8nd1tJlpxY1VdeoN7CksIySqhNijUurllZmkZHEou/OtSgMOjnvutPs5EOP/96AfY9sqBbHXuFRLa5FDuqO91A60s54b87LBQSU4srm64npKqxwXHCXQvRYid3quGKZp98hr7oG7yzIcRxv2ZseTlENoYrjRnQ9CwPtAVUhsKJhzk5v/0BQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://localhost:9000/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://localhost:9000/list?pageNum=1";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;
    }
}
